//
//  QueryViewController.m
//  JavaBaas_iOS_SDK
//
//  Created by zhaopeng on 16/1/27.
//  Copyright © 2016年 Buge. All rights reserved.
//

#import "QueryViewController.h"
#import "JavaBaas.h"

@implementation QueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (IBAction)saveClick:(UIButton *)sender {
    JBQuery *query = [JBQuery queryWithClassName:@"Test"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            JBObject *object = objects.firstObject;
            NSLog(@"%@",object);
            [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                    NSLog(@"%@",objects);
                }];
            }];
        }
    }];
//
    
//    JBObject *object = [JBObject objectWithClassName:@"Test"];
//    [object setObject:@"李四" forKey:@"title"];
//    //设置改条数据的acl权限
////    JBACL *acl = [JBACL ACL];
////    [acl setWriteAccess:YES forUserId:@"2bdf4e7b393c497f8d36529504177aeb"];
////    [acl setPublicReadAccess:YES];
////    object.acl = acl;
//    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//        if (!error) {
//            NSLog(@"save succeeded %@", object);
//        }else {
//            NSLog(@"%@", error);
//        }
//    }];
}

- (IBAction)deleteClick:(UIButton *)sender {
    JBObject *object = [JBObject objectWithoutDataWithClassName:@"Test" objectId:@"9ada59db2dc64655b3e03432bb62c1d8"];
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            NSLog(@"delete succeeded");
        }else {
            NSLog(@"%@", error);
        }
    }];
}

- (IBAction)updateClick:(UIButton *)sender {
    JBObject *object = [JBObject objectWithoutDataWithClassName:@"Test" objectId:@"9ada59db2dc64655b3e03432bb62c1d8"];
    [object setObject:@"aaaaa" forKey:@"title"];
    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            NSLog(@"update succeeded");
        }else {
            NSLog(@"%@", error);
        }
    }];
}

- (IBAction)queryClick:(UIButton *)sender {
    JBQuery *query = [JBQuery queryWithClassName:@"Test"];
    [query whereKey:@"title" equalTo:@"this is title"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            NSLog(@"Successfully retrieved %lu posts.", (unsigned long)objects.count);
        } else {
            NSLog(@"Error: %@", error);
        }
    }];
}
@end











